package com.zhike.b2b.product.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.zhike.b2b.base.BaseService;
import com.zhike.b2b.model.ClassVo;
import com.zhike.b2b.model.NewsClassVo;
import com.zhike.b2b.model.ProductClassVo;
import com.zhike.b2b.product.IProdClassService;
import com.zhike.b2b.util.ClassUtil;
import com.zhike.b2b.util.StringUtil;

@Service
public class ProdClassService extends BaseService implements IProdClassService{

	/**
	 * 根据条件查询产品(供应)类别列表
	 * @param clzLevel
	 * @param limitStr
	 * @param orderCln
	 * @param commend
	 * @return
	 */
	public List<ClassVo> queryProductClzs(String clzId,String limitStr,final boolean isRecursion)
	{
		List<ClassVo> lpcv = jt.query(getSqlstr(clzId,limitStr,"","",""),  new RowMapper<ClassVo>() {
			 public ProductClassVo mapRow(ResultSet rs, int rowNum)
				throws SQLException {
				 ProductClassVo pcv = new ProductClassVo();
				 pcv.setClassId(rs.getString("class_id"));
				 pcv.setListRule(rs.getString("list_rule"));
				 pcv.setStorePath(rs.getString("store_path"));
				 pcv.setHomeName(rs.getString("home_name"));
				 pcv.setDataCount(rs.getInt("datacount"));
				 pcv.setClassName(rs.getString("class_name"));
				 pcv.setListUrl(ClassUtil.getListUrl(pcv, 1));
				 if(isRecursion && pcv.getClassId().length()<10)
					 pcv.setSubClzs(queryProductClzs(pcv.getClassId(),"",isRecursion));
				 return pcv;
			}});
		return lpcv;
	}
	
	/**
	   * 按首字母获取所有产品(供应)类别列表
	   * @param paramString1
	   * @param paramString2
	   * @return
	   */
	  public Map<String,List<ProductClassVo>> getClassNameByChar()
	  {
		List<ProductClassVo> allPcs = getAllProdClass();
		Map<String,List<ProductClassVo>> mslp = new HashMap<String,List<ProductClassVo>>();

	    for(char chr = 'a';chr<='z';chr++){
	    	List<ProductClassVo> lpc = new ArrayList<ProductClassVo>();
			 for (ProductClassVo pcv : allPcs)
			 {
			     String f = StringUtil.getFirstStr(pcv.getClassName());
			     if(f.toLowerCase().equals(chr+""))
			     {
			    	 lpc.add(pcv) ;
			     }
			 }
			 mslp.put(chr+"", lpc);
		}
	    return mslp;
	  }
	  
	  /**
		 * 获取所有的产品(供应)分类列表
		 * @return
		 */
		private List<ProductClassVo> getAllProdClass()
		{
			String sql = "  select bc.class_id,bc.class_name,bc.store_path,bc.home_name,bc.list_rule from product_class bc where bc.remove_tag = 1 ";
			List<ProductClassVo> lpcs = jt.query(sql,  new RowMapper<ProductClassVo>() {
				 public ProductClassVo mapRow(ResultSet rs, int rowNum)
					throws SQLException {
					 ProductClassVo pcv = new ProductClassVo();
					 pcv.setClassId(rs.getString("class_id"));
					 pcv.setListRule(rs.getString("list_rule"));
					 pcv.setStorePath(rs.getString("store_path"));
					 pcv.setClassName(rs.getString("class_name"));
					 pcv.setListUrl(ClassUtil.getListUrl(pcv, 1));
					 return pcv;
				}
				 });
			 return lpcs;
		}
		
		/**
		 * 根据类别ID查询产品(供应)类别
		 * @param classId
		 * @return
		 */
		public ClassVo getClassById(String classId)
		{
			String sql = "select class_name,class_id,list_rule,store_path,index_model,home_name from  product_class where class_id = ? ";
			return jt.queryForObject(sql,  new RowMapper<ClassVo>() {
				 public ClassVo mapRow(ResultSet rs, int rowNum)
					throws SQLException {
					 ClassVo ncv = new ClassVo();
					 ncv.setClassId(rs.getString("class_id"));
					 ncv.setClassName(rs.getString("class_name"));
					 ncv.setListRule(rs.getString("list_rule"));
					 ncv.setStorePath(rs.getString("store_path"));
					 ncv.setHomeName(rs.getString("home_name"));
					 ncv.setIndexModel(rs.getString("index_model"));
					 return ncv;
				}}, classId);
		}
		
	/**
	 * 获得查询产品(供应)类别SQL
	 * @param clzLevel
	 * @param clzType
	 * @param page
	 * @param custId
	 * @param commend
	 * @return
	 */
	private String getSqlstr(String clzType, String page, String custId, String commend,String clzLevel)
	  {
	    if (clzType.indexOf("recommended-") > -1)
	    {
	      clzType = clzType.replaceAll("recommended-", "");
	    }
	    String length = "3";
	    int i = 0;
	    if (!clzLevel.equals(""))
	      i = Integer.parseInt(clzLevel);
	    logger.debug("class_level:" + clzLevel);
	    logger.debug("classtype:" + clzType);
	    switch (i)
	    {
	    case 1:
	      length = "3";
	      break;
	    case 2:
	      length = "6";
	      break;
	    case 3:
	      length = "10";
	      break;
	    case 4:
	      length = "15";
	      break;
	    case 5:
	      length = "20";
	      break;
	    default:
	      length = "0";
	    }
	    if (clzType.length() == 0 )
	    	length = "3";
	    else if (clzType.length() == 3)
	    	length = "6";
	    else if (clzType.length() == 6)
	    	length = "10";
	    else if (clzType.length() == 10)
	    	length = "15";
	    else if (clzType.length() == 15)
	    	length = "20";
	 
	    StringBuffer buf = new StringBuffer("select ifnull(p.datacount,0) datacount , bc.class_id,bc.class_name,bc.store_path,bc.home_name,bc.list_rule from product_class bc " +
	    		" left join (select count(left(class_id,"+length+")) as datacount,left(class_id,"+length+") class_id from product where  display_start_date <=now() and display_end_date >=now() and display=1 and validity =1  ");
	    if (commend.equals("1"))
	    	buf.append(" and commend= 1 ");
	    if (!custId.equals(""))
	    	buf.append(" and cust_id=" + custId);
	    buf.append("  group by left(class_id,"+length+")) as p on p.class_id =bc.class_id  where bc.checked=1 and  bc.remove_tag=1 and bc.class_id like '" + clzType + "%' ");  
	    buf.append(" and length(bc.class_id)=" + length );
	    if (!page.equals(""))
	    	buf.append(" limit " + page);
	  	    
	    logger.debug("产品类别："+buf.toString());
	    return buf.toString();
	  }
}
